﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using FineUI;
using SDLITS.Model;
using SDLITS.Common;
using MySoft.Data;

namespace SDLITS.Web.SystemManage
{
    public partial class AnnouncementManage : CommonBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnNew.OnClientClick = Window1.GetShowReference("AnnouncementChange.aspx", "新增公告");
                BindGrid();
            }
        }
        private void BindGrid()
        {
            Grid1.RecordCount = SDDbSession.Default.From<V_AnnounceUser>().Count();
            List<V_AnnounceUser> items = SDDbSession.Default.From<V_AnnounceUser>().Page(Grid1.PageSize, (Grid1.PageIndex + 1)).ToList();
            Grid1.DataSource = items;
            Grid1.DataBind();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }
       
        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
            if (e.CommandName == "Del")
            {
                SDDbSession.Default.Delete<Announcement>(Announcement._.AnnID == id);
                BindGrid();
            }
            if (e.CommandName == "dp")
            {
                CheckBoxField checkField = (CheckBoxField)Grid1.FindColumn(e.ColumnIndex);
                bool checkState = checkField.GetCheckedState(e.RowIndex);
                SDDbSession.Default.Update<Announcement>(Announcement._.IsHot, checkState, Announcement._.AnnID == id);
                BindGrid();
            }
            if (e.CommandName == "jh")
            {
                SDDbSession.Default.Update<Announcement>(Announcement._.StatusID, 1, Announcement._.AnnID == id);
                BindGrid();
            }
            if (e.CommandName == "dj")
            {
                SDDbSession.Default.Update<Announcement>(Announcement._.StatusID, 2, Announcement._.AnnID == id);
                BindGrid();
            }
        }

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            e.Values[2]=Methed.Status(e.Values[2].ToString());
        }
    }
}
